package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.IssueCreateBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketIssue;
import com.cskaoyan.bean.pojo.MarketIssueExample;
import com.cskaoyan.bean.vo.IssueListVO;
import com.cskaoyan.mapper.MarketIssueMapper;
import com.cskaoyan.service.IssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

/**
 * 商场管理-通用问题 的Service实现类
 * 创建日期: 2022/04/07 20:11
 *
 * @author ChengWenqi
 */

@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    MarketIssueMapper issueMapper;

    @Override
    public IssueListVO queryIssueList(BaseParam param, String question) {
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        // 开启分页
        PageHelper.startPage(page, limit);

        // 排序
        MarketIssueExample example = new MarketIssueExample();
        example.setOrderByClause(sort + " " + order);
        // question模糊查询
        MarketIssueExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (question != null && !("".equals(question))) {
            criteria.andQuestionLike("%" + question + "%");
        }
        // 执行查询语句
        List<MarketIssue> issueList = issueMapper.selectByExample(example);

        // 获取分页信息
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(issueList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        return new IssueListVO(total, pages, limit, page, issueList);
    }

    @Transactional
    @Override
    public MarketIssue issueCreate(IssueCreateBO issueCreateBO) {
        @NotBlank String question = issueCreateBO.getQuestion();
        @NotBlank String answer = issueCreateBO.getAnswer();
        MarketIssue issue = new MarketIssue(null, question, answer, new Date(), new Date(), false);
        // 将用户输入的问题插入数据库中，并获得自增的主键，放到id里
        issueMapper.insert(issue);
        // 根据刚刚插入的记录的主键，获取记录信息
        return issueMapper.selectByPrimaryKey(issue.getId());
    }

    @Transactional
    @Override
    public MarketIssue issueUpdate(MarketIssue issue) {
        Integer id = issue.getId();
        // 先根据id查找对应记录，如果没查到，或者deleted=1，设置id为0并返回
        MarketIssue oldIssue = issueMapper.selectByPrimaryKey(id);
        if (oldIssue == null || oldIssue.getDeleted()) {
            issue.setId(0);
            return issue;
        }
        // 查到了记录，判断问题是否完全一致，如果不一致，返回null
        if (!oldIssue.getQuestion().trim().equals(issue.getQuestion().trim())) {
            return null;
        }
        // 问题一致，将用户提交的issue的updateTime更新，更新数据库记录
        issue.setUpdateTime(new Date());
        issueMapper.updateByPrimaryKey(issue);
        return issue;
    }

    @Override
    public int issueDelete(MarketIssue issue) {
        issue.setDeleted(true);
        issue.setUpdateTime(new Date());
        int update = issueMapper.updateByPrimaryKey(issue);
        if (update == 1) {
            return 200;
        }
        return 404;
    }
}
